package com.lj.luoye.lotteryticket.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lj.luoye.lotteryticket.entity.Unionlotto;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class LotteryBuyDao {
    public final static String table_name = "lottery_buy";
    public DBHelper dbHelper;
    public Context context;
    String[] fields = new String[]{"id","date","description", "redBall1", "redBall2", "redBall3", "redBall4", "redBall5", "redBall6", "blueBall","bought","luckResult"};

    public LotteryBuyDao(Context context) {
        this.dbHelper = new DBHelper(context);
        this.context = context;
    }

    public void saveUnionlottos(String date,List<Unionlotto> unionlottoList){
        //先查询，如果有了的话就先删除
        List<Unionlotto> unionlottoListGet = getByDate(date);
        if(unionlottoListGet.size()>0){
            deleteByDate(date);
        }
        for(Unionlotto unionlotto:unionlottoList){
            unionlotto.date=date;
            insert(unionlotto);
        }
    }

    public long updateUnionlottoToBought(String date){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("bought",1);
        long res = db.update(LotteryBuyDao.table_name,contentValues, "date=?", new String[]{date});
        db.close();
        return res;
    }

    public long deleteByDate(String date){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        long res = db.delete(LotteryBuyDao.table_name, "date=?", new String[]{date});
        db.close();
        return res;
    }

    public List<String> getDates(){
        List<String> dates=new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(LotteryBuyDao.table_name,fields,null,null,"date",null,null);
        while(cursor.moveToNext()){
            String date = cursor.getString(cursor.getColumnIndex("date"));
            dates.add(date);
        }
        db.close();
        return dates;
    }

    public List<String> getMonths(){
        List<String> dates=new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(LotteryBuyDao.table_name,fields,null,null,"date",null,null);
        while(cursor.moveToNext()){
            String date = cursor.getString(cursor.getColumnIndex("date"));
            dates.add(date);
        }
        db.close();
        Set<String> months=new HashSet<>();
        for(String date:dates){
            String month=date.substring(0,7);
            months.add(month);
        }
        List<String> monthList=new ArrayList<>();
        monthList.addAll(months);
        return monthList;
    }

    public List<Unionlotto> getByDate(String date){
        List<Unionlotto> unionlottoList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(LotteryBuyDao.table_name,fields,"date=?",new String[]{date},null,null,null);
        while(cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String description = cursor.getString(cursor.getColumnIndex("description"));
            String redBall1 = cursor.getString(cursor.getColumnIndex("redBall1"));
            String redBall2 = cursor.getString(cursor.getColumnIndex("redBall2"));
            String redBall3 = cursor.getString(cursor.getColumnIndex("redBall3"));
            String redBall4 = cursor.getString(cursor.getColumnIndex("redBall4"));
            String redBall5 = cursor.getString(cursor.getColumnIndex("redBall5"));
            String redBall6 = cursor.getString(cursor.getColumnIndex("redBall6"));
            String blueBall = cursor.getString(cursor.getColumnIndex("blueBall"));
            int bought = cursor.getInt(cursor.getColumnIndex("bought"));
            String luckResult = cursor.getString(cursor.getColumnIndex("luckResult"));
            List<String> redBallList=new ArrayList<>();
            redBallList.add(redBall1);
            redBallList.add(redBall2);
            redBallList.add(redBall3);
            redBallList.add(redBall4);
            redBallList.add(redBall5);
            redBallList.add(redBall6);
            Unionlotto unionlotto = new Unionlotto(id,date,description,redBallList,blueBall,bought,luckResult);
            unionlottoList.add(unionlotto);
        }
        db.close();
        return unionlottoList;
    }

    public int insert(Unionlotto unionlotto){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("date",unionlotto.date);
        contentValues.put("description",unionlotto.description);
        int i=1;
        for(String redBall:unionlotto.getRedBalls()){
            contentValues.put("redBall"+i,redBall);
            i++;
        }
        contentValues.put("blueBall",unionlotto.blueBall);
        contentValues.put("bought",unionlotto.bought);
        contentValues.put("luckResult",unionlotto.luckResult);
        long id = db.insert(LotteryBuyDao.table_name, null, contentValues);
        db.close();
        return (int) id;
    }

    public int updateLuckResult(int id,String luckResult){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("luckResult",luckResult);
        int res = db.update(LotteryBuyDao.table_name, contentValues, "id=?", new String[]{id+""});
        db.close();
        return res;
    }

    public List<Unionlotto> getBoughtByMonth(String month){
        List<Unionlotto> unionlottoList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(LotteryBuyDao.table_name,fields,"bought=1 and date like '"+month+"%'",new String[]{},null,null,null);
        while(cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String date = cursor.getString(cursor.getColumnIndex("date"));
            String description = cursor.getString(cursor.getColumnIndex("description"));
            String redBall1 = cursor.getString(cursor.getColumnIndex("redBall1"));
            String redBall2 = cursor.getString(cursor.getColumnIndex("redBall2"));
            String redBall3 = cursor.getString(cursor.getColumnIndex("redBall3"));
            String redBall4 = cursor.getString(cursor.getColumnIndex("redBall4"));
            String redBall5 = cursor.getString(cursor.getColumnIndex("redBall5"));
            String redBall6 = cursor.getString(cursor.getColumnIndex("redBall6"));
            String blueBall = cursor.getString(cursor.getColumnIndex("blueBall"));
            int bought = cursor.getInt(cursor.getColumnIndex("bought"));
            String luckResult = cursor.getString(cursor.getColumnIndex("luckResult"));
            List<String> redBallList=new ArrayList<>();
            redBallList.add(redBall1);
            redBallList.add(redBall2);
            redBallList.add(redBall3);
            redBallList.add(redBall4);
            redBallList.add(redBall5);
            redBallList.add(redBall6);
            Unionlotto unionlotto = new Unionlotto(id,date,description,redBallList,blueBall,bought,luckResult);
            unionlottoList.add(unionlotto);
        }
        db.close();
        return unionlottoList;
    }

    public List<Unionlotto> getAll(){
        List<Unionlotto> unionlottoList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(LotteryBuyDao.table_name,fields,"",new String[]{},null,null,null);
        while(cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String date = cursor.getString(cursor.getColumnIndex("date"));
            String description = cursor.getString(cursor.getColumnIndex("description"));
            String redBall1 = cursor.getString(cursor.getColumnIndex("redBall1"));
            String redBall2 = cursor.getString(cursor.getColumnIndex("redBall2"));
            String redBall3 = cursor.getString(cursor.getColumnIndex("redBall3"));
            String redBall4 = cursor.getString(cursor.getColumnIndex("redBall4"));
            String redBall5 = cursor.getString(cursor.getColumnIndex("redBall5"));
            String redBall6 = cursor.getString(cursor.getColumnIndex("redBall6"));
            String blueBall = cursor.getString(cursor.getColumnIndex("blueBall"));
            int bought = cursor.getInt(cursor.getColumnIndex("bought"));
            String luckResult = cursor.getString(cursor.getColumnIndex("luckResult"));
            List<String> redBallList=new ArrayList<>();
            redBallList.add(redBall1);
            redBallList.add(redBall2);
            redBallList.add(redBall3);
            redBallList.add(redBall4);
            redBallList.add(redBall5);
            redBallList.add(redBall6);
            Unionlotto unionlotto = new Unionlotto(id,date,description,redBallList,blueBall,bought,luckResult);
            unionlottoList.add(unionlotto);
        }
        db.close();
        return unionlottoList;
    }

    public int importRow(Unionlotto unionlotto){
        //先查询，是否已经存在
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(LotteryBuyDao.table_name,fields,"date=? and redBall1=? and redBall2=? and redBall3=? and redBall4=? and redBall5=? and redBall6=? and blueBall=? ",
                new String[]{unionlotto.date,unionlotto.redBalls.get(0),unionlotto.redBalls.get(1),unionlotto.redBalls.get(2),unionlotto.redBalls.get(3),unionlotto.redBalls.get(4),
                        unionlotto.redBalls.get(5),unionlotto.blueBall},null,null,null);
//        Cursor cursor = db.query(LotteryBuyDao.table_name,fields,"date=? ",
//                new String[]{unionlotto.date},null,null,null);
        int count=cursor.getCount();
        db.close();
        System.out.println("count->"+count);
        if(count>0){
            return 0;
        }
        return insert(unionlotto);
    }


}
